package com.retlife.tvshow.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.retlife.tvshow.R

class PlayerView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    private var isActive: Boolean = false
    private var speed: Int = 0
    private var boolean: Boolean = false

    private var lastProcess: Int = 0
    private var player: Int = 1
    private var stopTime = 0
    private var process: Int = 0
    private var totalProcess: Int = 0
    private var z = 0
    private val p1: Bitmap
    private val p2: Bitmap
    private val p3: Bitmap
    private val p4: Bitmap
    private val r1: Bitmap
    private val r2: Bitmap
    private val r3: Bitmap
    private val wm1: Bitmap
    private val wm2: Bitmap
    private var iconIndex: Bitmap
    private val paint = Paint()
    private lateinit var iconIndexCopy: Bitmap
    private lateinit var p1Copy: Bitmap
    private lateinit var p2Copy: Bitmap
    private lateinit var p3Copy: Bitmap
    private lateinit var p4Copy: Bitmap
    private lateinit var r1Copy: Bitmap
    private lateinit var r2Copy: Bitmap
    private lateinit var r3Copy: Bitmap
    private lateinit var wm1Copy: Bitmap
    private lateinit var wm2Copy: Bitmap
    private val matrix = Matrix()
    private var oldew = 0
    private var oldeh = 0
    init {
        setLayerType(LAYER_TYPE_SOFTWARE, null)
        iconIndex = BitmapFactory.decodeResource(resources, R.mipmap.icon_1)
        p1 = BitmapFactory.decodeResource(resources, R.mipmap.p1)
        p2 = BitmapFactory.decodeResource(resources, R.mipmap.p2)
        p3 = BitmapFactory.decodeResource(resources, R.mipmap.p3)
        p4 = BitmapFactory.decodeResource(resources, R.mipmap.p4)
        r3 = BitmapFactory.decodeResource(resources, R.mipmap.r3)
        r2 = BitmapFactory.decodeResource(resources, R.mipmap.r2)
        r1 = BitmapFactory.decodeResource(resources, R.mipmap.r1)
        wm1 = BitmapFactory.decodeResource(resources, R.mipmap.wm1)
        wm2 = BitmapFactory.decodeResource(resources, R.mipmap.wm2)
        paint.isAntiAlias = true
        paint.isFilterBitmap = true
    }

    fun setProcess(process: Int, i: Int) {
        this.process = process
        z++
        this.totalProcess = i
        /*  if (totalProcess==lastProcess){
              stopTime++
          }else{
              stopTime = 0
          }*/
        postInvalidate()

        lastProcess = i

    }

    fun getProcess(): Int {

        return process
    }

    fun setPlayer(player: Int) {
        this.player = player
        val indexResId = when (player) {
            0 -> R.mipmap.icon_1
            1 -> R.mipmap.icon_2
            2 -> R.mipmap.icon_3
            3 -> R.mipmap.icon_4
            4 -> R.mipmap.icon_5
            5 -> R.mipmap.icon_6
            6 -> R.mipmap.icon_7
            7 -> R.mipmap.icon_8
            else -> R.mipmap.icon_8
        }
        iconIndex = BitmapFactory.decodeResource(resources, indexResId, BitmapFactory.Options())
    }

    @SuppressLint("DrawAllocation")
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (measuredHeight <= 0||measuredWidth<=0)
            return
        if (measuredHeight==oldeh&&measuredWidth==oldew)
            return
        oldeh = measuredHeight
        oldew = measuredWidth
        val scale: Float = (measuredHeight * 1.0f) / (p1.height * 1.0f)
        matrix.setScale(scale, scale)
        p1Copy = Bitmap.createBitmap(p1, 0, 0, p1.width, p1.height, matrix, true)
        p2Copy = Bitmap.createBitmap(p2, 0, 0, p2.width, p2.height, matrix, true)
        p3Copy = Bitmap.createBitmap(p3, 0, 0, p3.width, p3.height, matrix, true)
        p4Copy = Bitmap.createBitmap(p4, 0, 0, p4.width, p4.height, matrix, true)
        val scale1: Float = (p1Copy.height * 0.35f) / (wm1.height * 1.0f)
        matrix.setScale(scale1, scale1)
        wm1Copy = Bitmap.createBitmap(wm1, 0, 0, wm1.width, wm1.height, matrix, true)
        wm2Copy = Bitmap.createBitmap(wm2, 0, 0, wm2.width, wm2.height, matrix, true)
        val scale2: Float = (p1Copy.height * 0.112f) / (r1.height * 1.0f)
        matrix.setScale(scale2, scale2)
        r1Copy = Bitmap.createBitmap(r1, 0, 0, r1.width, r1.height, matrix, true)
        r2Copy = Bitmap.createBitmap(r2, 0, 0, r2.width, r2.height, matrix, true)
        r3Copy = Bitmap.createBitmap(r3, 0, 0, r3.width, r3.height, matrix, true)
        val scale3: Float = (measuredHeight * 0.25f) / (iconIndex.height * 1.0f)
        matrix.setScale(scale3, scale3)
        iconIndexCopy = Bitmap.createBitmap(iconIndex, 0, 0, iconIndex.width, iconIndex.height, matrix, true)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        /* paint.color = if (speed<10){
             resources.getColor(R.color.green)
         }else if (speed>20){
             resources.getColor(R.color.red)

         }else{
             resources.getColor(R.color.yellow)
         }
         canvas.drawText("速度:${speed}", (width * 8 / 10).toFloat(), (height - 4).toFloat(), paint)
         if (stopTime > 50) {
             paint.color = resources.getColor(R.color.purple_200)
             canvas.drawText("玩家${player + 1}已退出游戏", 20f, (height - 4).toFloat(), paint)
             return
         }
         if (process < -2000) {
             paint.color = resources.getColor(R.color.purple_200)
             paint.textSize = (height - 10).toFloat()

 //            canvas.drawText("玩家${player + 1}已掉队,加油追上来", 20f, (height - 4).toFloat(), paint)
             canvas.drawText("hurry up!", 20f+ Random.nextInt(5), (height - 10)+Random.nextInt(5).toFloat(), paint)
             return
         }*/
        /*   val int: Int = if (z % 30 < 8) {
               R.mipmap.p1
           } else if (z % 30 < 15) {
               R.mipmap.p2
           } else if (z % 30 < 23) {
               R.mipmap.p3
           } else {
               R.mipmap.p4
           }
           val modeResource =
               if (isActive) {
                   if (z % 30 < 15)
                       R.mipmap.wm1
                   else
                       R.mipmap.wm2

               } else {
                   if (z % 30 < 10)

                       R.mipmap.r1
                   else if (z % 30 < 20)
                       R.mipmap.r2
                   else
                       R.mipmap.r3

               }
             val bitmap: Bitmap = BitmapFactory.decodeResource(resources, int, BitmapFactory.Options())
             val bitmapMode: Bitmap = BitmapFactory.decodeResource(resources, modeResource, BitmapFactory.Options())

             val matrix = Matrix()
             val scale: Float = (canvas.height * 1.0f) / (bitmap.height * 1.0f)
             matrix.setScale(scale, scale)
             val bitmapCopy = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)*/
        val bitmapCopy = if (z % 30 < 8) {
            p1Copy
        } else if (z % 30 < 15) {
            p2Copy
        } else if (z % 30 < 23) {
            p3Copy
        } else {
            p4Copy
        }
        val value = width * (process / 10000f)
        canvas.drawBitmap(bitmapCopy, value, 0f, paint)
        /*val matrix1 = Matrix()
//        val scale1: Float = (canvas.height * 1.0f) / (bitmapMode.height * 1.0f)
        if (isActive) {
            val scale1: Float = (bitmapCopy.height * 0.35f) / (bitmapMode.height * 1.0f)
            matrix1.setScale(scale1, scale1)
        } else {
            val scale1: Float = (bitmapCopy.height * 0.112f) / (bitmapMode.height * 1.0f)
            matrix1.setScale(scale1, scale1)
        }
        val bitmapCopy1 = Bitmap.createBitmap(bitmapMode, 0, 0, bitmapMode.width, bitmapMode.height, matrix1, true)*/
        val bitmapCopy1 = if (isActive) {
            if (z % 30 < 15)
                wm1Copy
            else
                wm2Copy

        } else {
            if (z % 30 < 10)
                r1Copy
            else if (z % 30 < 20)
                r2Copy
            else
                r3Copy

        }
        if (isActive)
            canvas.drawBitmap(bitmapCopy1, value - bitmapCopy1.width * 0.77f, (height - bitmapCopy1.height).toFloat(), null)
        else
            canvas.drawBitmap(bitmapCopy1, value - bitmapCopy1.width * 0.70f, (height - bitmapCopy1.height * 1.6f), null)

        /* paint.textSize = (height * 0.25f - 14)

         paint.color = resources.getColor(R.color.black)

         canvas.drawText("${player + 1}", bitmapCopy.width * 0.56f + value, (height * 0.25f - 14), paint)
 */
        /* val indexResId = when (player) {
             0 -> R.mipmap.icon_1
             1 -> R.mipmap.icon_2
             2 -> R.mipmap.icon_3
             3 -> R.mipmap.icon_4
             4 -> R.mipmap.icon_5
             5 -> R.mipmap.icon_6
             6 -> R.mipmap.icon_7
             7 -> R.mipmap.icon_8
             else -> R.mipmap.icon_8
         }
         val bitmapIndex: Bitmap = BitmapFactory.decodeResource(resources, indexResId, BitmapFactory.Options())
         val matrix2 = Matrix()
         val scale2: Float = (canvas.height * 0.25f) / (bitmapIndex.height * 1.0f)
         matrix2.setScale(scale2, scale2)
         val bitmapCopy2 = Bitmap.createBitmap(bitmapIndex, 0, 0, bitmapIndex.width, bitmapIndex.height, matrix2, true)*/
        canvas.drawBitmap(iconIndexCopy, value + (bitmapCopy.width) * 0.46f, 0f, null)
//        boolean = !boolean
    }

    fun setSpeed(i: Int) {
        this.speed = i
    }

    fun setActive(b: Boolean) {
        this.isActive = b
    }
}